declare var Physijs;
module m {
	/**
	 * 游戏场景
	 */
	export class gameScene extends egret.Sprite {
		public Stats: any;
		public constructor() {
			super();
			document.getElementById('world').style.display = 'block';
			let script = document.createElement('script');
			script.type = 'text/javascript';
			script.src = 'https://fs-ews.oss-cn-hangzhou.aliyuncs.com/main/m/ballshot_m/stats.min.js';
			document.getElementsByTagName('head')[0].appendChild(script);
			script.onload = function () {
				console.log('方法加载完成');
				this.Stats = window['Stats']()
				document.getElementById('world').appendChild(this.Stats.dom);
				this.Stats.dom.style.top = 150 + 'px';
				// console.log(this.Stats.dom)
			}.bind(this)
			this.create();
		}
		//文件加载
		public textLoader: any;
		//陆地
		public Ground: Ground;
		//球门
		public goalDoor: goalDoor;
		public create() {
			this.initThree();
			// gameScene.Scene = window['scene']
			this.createMaterial();

			this.Ground = new Ground(this);
			this.addChild(this.Ground);

			this.goalDoor = new goalDoor(this);
			this.addChild(this.goalDoor);

			this.addEventListener(egret.Event.ENTER_FRAME, this.loop, this);
		}

		/**
		 * 预加载
		 */
		public createMaterial() {
			this.textLoader = new THREE.TextureLoader();
		}

		//渲染器
		public Renderer: any;
		public Camera: any;
		static Scene: any;
		public Light: any;
		/**
		 * 初始化three场景
		 */
		public initThree() {
			Physijs.scripts.worker = 'resource/physijs_worker.js';
			Physijs.scripts.ammo = '../resource/ammo.js';
			//场景
			gameScene.Scene = new Physijs.Scene({
				reportsize: 50,
				fixedTimeStep: 1 / 60
			})
			gameScene.Scene.setGravity(new THREE.Vector3(0, -30, 0));
			gameScene.Scene.background = new THREE.Color(0xcce0ff);
			gameScene.Scene.fog = new THREE.Fog(0xcce0ff, 500, 10000);
			//相机
			this.Camera = new THREE.PerspectiveCamera(60, 1366 / 768, 0.1, 1000);
			this.Camera.position.set(0, 2, 4);
			this.Camera.lookAt(0, 2, -6);
			//渲染器
			this.Renderer = new THREE.WebGLRenderer({
				antialias: true,
				alpha: true
			})
			this.Renderer.setSize(window.innerWidth, window.innerHeight);
			document.getElementById('world').appendChild(this.Renderer.domElement);
			this.Renderer.shadowMapEnabled = true;
			this.Renderer.gammaInput = true;
			this.Renderer.gammaOutput = true;
			//灯光
			var light = new THREE.AmbientLight(0xffffff, 0.3); // soft white light
			light.position.set(6, 6, 0);
			gameScene.Scene.add(light);
			//点光源
			var light1 = new THREE.PointLight(0xffffff, 1, 0);
			light1.position.set(0, 10, 10);
			light1.castShadow = true;
			gameScene.Scene.add(light1);
		}

		/**
		 * 帧事件
		 */
		public loop(e: egret.Event) {
			// this.Renderer.render(gameScene.Scene, this.Camera);
			// gameScene.Scene.simulate();
			window['Rr']();
		}

		/**
		 * 销毁
		 */
		public destory() {

		}
	}
}